package com.ymatou.iapi.productmanager.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.productcategory.parameter.ProductCategoryDto2;
import com.ymatou.iapi.productcategory.parameter.SaveProductCategoryBean;
import com.ymatou.iapi.productcategory.service.SaveProductCategoryCall;
import com.ymatou.iapi.productmanager.parameter.GetProductCategoryListBean;
import com.ymatou.iapi.productmanager.parameter.ProductCategoryListBean;
import com.ymatou.iapi.productmanager.service.GetProductCategoryListCall;
import com.ymt.base.BaseBean;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ProductManagerCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.product.IntergratedProductMapper;
import com.ymttest.database.mongo.BuyerProductWapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.compare.AssertHelper;

public class Ts_GetProductCategoryList {
	private static GetProductCategoryListCall getProductCategoryListCall = new GetProductCategoryListCall();
	IntergratedProductMapper ipm;
	BuyerProductWapper BuyerProductWapper;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取全部商品类目列表");
	}
	
	@Before
	public void caseUp() {
		try {
			BuyerProductWapper=new BuyerProductWapper();
			ipm= SqlMapperFactory
					.getIntergratedProductMapper();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getProductCategoryListCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	
	

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_001() {
		Logger.start(true,"获取全部商品类目列表-查所有,根节点");
		try{
			checkGetProductCatalogList(-1);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_002() {
		Logger.start(true,"获取全部商品类目列表-查level1");
		try{
			checkGetProductCatalogList(0);
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_003() {
		Logger.start(true,"获取全部商品类目列表-查level2");
		try{
			checkGetProductCatalogList(1051);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_004() {
		Logger.start(true,"获取全部商品类目列表-查level3");
		try{
			checkGetProductCatalogList(1081);
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_005() {
		Logger.start(true,"获取全部商品类目列表-查不存在");
		try{
			JSONObject ret = getProductCategoryListCall.callAndGetReturnData(MapUtil.hashMap("parentId",123123123));
			Logger.verifyEquals(true, ret.getJSONObject("Data").isNull("categoryList"), "不存在id，所有返回应为null");
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_GetProductCategoryList_006() {
		Logger.start(true,"获取全部商品类目列表-高危");
		try{
			ProductCategoryDto2 c=getNewCategory();
			GetProductCategoryListBean bean=new GetProductCategoryListBean();
			bean.setParentId(0);
			getProductCategoryListCall.setData(bean);
			getProductCategoryListCall.callService();
			getProductCategoryListCall.getJsonObject().getAsJsonObject("Data").get("categoryList").getAsJsonArray().forEach(x->{
				if(x.getAsJsonObject().get("categoryId").getAsInt()==c.getCategoryId()) {
					Logger.debug(x.toString());
					Logger.verifyEquals(true, x.getAsJsonObject().get("risk").getAsBoolean(), "高危");
				}
			});
		}catch(Exception e){
			Logger.fail(e);
		}
	}

	/**
	 * 清理类目
	 * @param cid
	 * @throws Exception
	 */
	private void cleanData(Integer cid) throws Exception {
		if(cid==null) {
			return;
		}
		BuyerProductWapper.deleteProductCatalogInfoByCid(cid);
		ipm.deleteYmtProductCategory(cid);
		ipm.deleteYMTCategoryTag(cid);
	}
	/**
	 * 初始化类目
	 * @return
	 */
	private ProductCategoryDto2 initProductCategoryDto() {
		ProductCategoryDto2 ProductCategoryDto2=new ProductCategoryDto2();
		ProductCategoryDto2.setbIsAvailable(true);
		ProductCategoryDto2.setbIsChildNodeId(true);
		ProductCategoryDto2.setCategoryEnName("CategoryEnName");
		ProductCategoryDto2.setCategoryId(10088);
		ProductCategoryDto2.setCategoryName("类目名");
		ProductCategoryDto2.setHighRisk(true);
		ProductCategoryDto2.setLevel(1);
		ProductCategoryDto2.setMasterCategory(0);
		ProductCategoryDto2.setOpeator("test");
		//url(//s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png)
		ProductCategoryDto2.setSamplePic("http://s2.ymatou.com/home/4aa0780b5ffeee46a01f132052fdb71a.png");
		ProductCategoryDto2.setSort(1);
		ProductCategoryDto2.setType(1);
		return ProductCategoryDto2;
	}
	/**
	 * 获取一个新的CategoryId
	 * @return
	 * @throws Exception 
	 */
	private ProductCategoryDto2 getNewCategory() throws Exception {
		List<ProductCategoryDto2> productCategoryList = new ArrayList<ProductCategoryDto2>();
		SaveProductCategoryBean SaveProductCategoryBean=new SaveProductCategoryBean();
		SaveProductCategoryCall SaveProductCategoryCall=new SaveProductCategoryCall();
		ProductCategoryDto2 productCategory=initProductCategoryDto();
		//类目不能重复 删除了再加
		cleanData(productCategory.getCategoryId());
		productCategoryList.add(productCategory);
		SaveProductCategoryBean.setProductCategoryList(productCategoryList);
		SaveProductCategoryCall.setData(SaveProductCategoryBean);
		SaveProductCategoryCall.callService();
		return productCategory;
	}
	
	
	public static void checkGetProductCatalogList(int parentId) throws Exception{
		
		JSONObject ret = getProductCategoryListCall.callAndGetReturnData(MapUtil.hashMap("parentId",parentId));
		
		ProductCategoryListBean actBean = ProductManagerCallService
										.getBeanFromJson(ret.getJSONObject("Data"),ProductCategoryListBean.class);
		
		ProductCategoryListBean tgtBean = ProductManagerCallService.getProductCategoryListBean(parentId);
		AssertHelper.assertResultEqual(tgtBean, actBean, "GetProductCategoryList");
		
	}
}
